// pages/goodsxq/goodsxq.js
const app = getApp();
Page({
  
  /**
   * 页面的初始数据
   */
  data: {
    // num:1,
    tabindex: '1',     //tab切换index
    id: 'maodian1',   //初始锚点id
    pageheight: '',  //滑动页面高
    sizexzbox: false,  //判断弹出框动画class
    blackzt: 'none',  //黑幕层显示/隐藏状态
    goodsid:'',
    type:'',  //1单独 2开团
    obj:{},
    actEndTimeList: [],
    countDownList:[],
    timer:'',
    pingjialist: [], //评价列表
    youxuanlist:[],
    haohuolist:[],
    guigemoney: '',
    sublist:[],
    guigeindex: 0, //规格索引
    guigelist: [], //规格列表
    isOverShare: true,
    shareimg:'',
    img1:'',
    img2:'',
    suretype:'',
    shopid:'',
    guigestr:"",
    totalnum:''

  },
  //输入购买数量
  totalnum(e){
    console.log(e)
    var totalnum = e.detail.value
    var obj = this.data.obj;
   
    var index = e.currentTarget.dataset.index;
    obj.goodsSku[index].num = totalnum
    this.setData({
      obj:obj
    })

  },

  // 去参团
  tojointuan(){
    if (!wx.getStorageSync('userid')) {
      wx.navigateTo({
        url: '../shouquan/shouquan',
      })
      return false;
    }
    var goodsid = this.data.goodsid;
    wx.navigateTo({
      url: '../jointuan/jointuan?goodsid=' + goodsid,
    })

    if (this.data.obj.tourState == 1){
      // wx.showToast({
      //   title: '你已在该组中',
      //   icon: 'none',
      //   duration: 2000
      // })
      
      // wx.navigateTo({
      //   url: '../jointuan/jointuan?goodsid=' + goodsid,
      // })
      // wx.showModal({
      //   title: '提示',
      //   content: '你已开拼此商品，是否邀请好友参团',
      //   success(res) {
      //     if (res.confirm) {
      //       console.log('用户点击确定')
      //       wx.navigateTo({
      //         url: '../toyaoqing/toyaoqing?goodsid=' + goodsid,
      //       })
      //     } else if (res.cancel) {
      //       console.log('用户点击取消')
      //     }
      //   }
      // })
      
    }else{
      
    }
    
  },
  topingjialist() {
    var goodsid = this.data.goodsid;
    wx.navigateTo({
      url: '../pingjialist/pingjialist?goodsid=' + goodsid,
    })
  },

  
  tokefu(){
    if (!wx.getStorageSync('userid')) {
      wx.navigateTo({
        url: '../shouquan/shouquan',
      })
      return false;
    }
    // wx.showToast({
    //   title: '暂未开通服务，敬请期待',
    //   icon:'none',
    //   duration:1500
    // })
    // 跳转客服
    var _this = this;
    var rctoken = this.data.obj.shopRcToken;
    var shopid = this.data.obj.shopId;
    console.log(rctoken)
    wx.request({
      url: "https://sdzxiangmu.com/ZJservice", //自己的服务接口地址
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data: {
        cmd: "getRectokenInfo",
        flag: 1,   //0-普通用户 1-店铺
        // userId: rctoken,   //普通用户ID (flag为0时必传)
        shopId: shopid,    //店铺ID    (flag为1时必传)
      },
      success: function success(res) {
        console.log(res.data)
        var name = res.data.beans[0].nickName;
        var img = res.data.beans[0].photo;
        var goodsInfo = {
          goodsId:_this.data.obj.goodsId,
          goodsImg: _this.data.obj.goodsImg,
          goodsName: _this.data.obj.goodsName,
          goodsPrice1: _this.data.obj.goodsCurPrice,
          goodsSales: _this.data.obj.goodsSales,
          mGoodsType: 5,
          shopCollect: _this.data.obj.shopCollect,
          tourList: _this.data.obj.tourList,
          tourGroupNum: _this.data.obj.tourGroupNum,
          tourNum: _this.data.obj.tourNum,
          tourState: _this.data.obj.tourState
        }
        let obj = JSON.stringify(goodsInfo)
        if (res.data.result == 0) {
          wx.navigateTo({
            url: '../kefu/kefu?friendId=' + rctoken + '&friendName=' + name + '&friendAvatarUrl=' + img + '&goodsinfo=' + obj + '&sharetype=1',
          })
        }
      }
    });
  },
  tocar(){
    if (!wx.getStorageSync('userid')) {
      wx.navigateTo({
        url: '../shouquan/shouquan',
      })
      return false;
    }
    wx.switchTab({
      url: '../../../pages/car/car',
    })
  },
  toshopxq(){
    var _this = this;
    wx.navigateTo({
      url: '../shopxq/shopxq?shopid='+_this.data.obj.shopId,
    })
  },

  togoodsxq(e){
    var type = e.currentTarget.dataset.type;
    var goodsid = e.currentTarget.dataset.goodsid;
    if(type == 5){
      wx.navigateTo({
        url: '../huopinxq/huopinxq?goodsid=' + goodsid + '&type=' + type, //本厂优选
      })
    }else{
      wx.navigateTo({
        url: '../goodsxq/goodsxq?goodsid=' + goodsid + '&type=' + type, //本厂优选
      })
    }
    
  },
  changenum(e){
    var _this = this;
    var obj = this.data.obj;
    var num = e.currentTarget.dataset.num;
    var type = e.currentTarget.dataset.type;
    var index = e.currentTarget.dataset.index;
    var totalnum = _this.data.totalnum;

    console.log(num)
    console.log(obj.goodsSku)
    console.log(_this.data.guigelist)
    if (type == 0) {
      if (num > 0) {
        obj.goodsSku[index].num--;
        //obj.goodsSku[index].num = totalnum
        this.setData({
          obj:obj,
          guigeindex:index
        })
      }
    } else {
      obj.goodsSku[index].num++;
      // totalnum++;
      // obj.goodsSku[index].num = totalnum
      this.setData({
        obj: obj,
        guigeindex: index
      })
    }
    var list = [];
    var sublist = [];
    for (var i in obj.goodsSku) {
      if (obj.goodsSku[i].num > 0) {
        list.push(obj.goodsSku[i].skuValue1)
        sublist.push({
          goodsId: obj.goodsId,
          skuId: obj.goodsSku[i].skuId,
          operType: 0, 
          goodsBuyNum: obj.goodsSku[i].num
        })
      }
    }
    var str = '';
    for (var l in list) {
      str = str + list[l] + ','
    }
    str = str.substring(0, str.length - 1);
    this.setData({
      guigestr:str,
      sublist: sublist,
      guigeindex: index
    })
  },
  //店铺收藏or取消
  shopshoucang() {
    if (!wx.getStorageSync('userid')) {
      wx.navigateTo({
        url: '../shouquan/shouquan',
      })
      return false;
    }
    var _this = this;
    var type = this.data.obj.shopCollect;
    if (type == 0) {
      type = 1
    } else {
      type = 0
    }
    console.log(type)
    wx.request({
      url: "https://sdzxiangmu.com/ZJservice", //自己的服务接口地址
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data: {
        cmd: "collect",
        userId: wx.getStorageSync('userid'),
        type: '1', //类型    0-厂家商品收藏 1-厂家店铺收藏
        cid: _this.data.obj.shopId, //商品id或店铺id
        isCollect: type //0取消 1收藏
      },
      success: function success(res) {
        console.log(res.data);
        wx.showToast({
          title: res.data.resultNote,
          icon: 'none',
          duration: 1500
        })
        if (res.data.result == 0) {
          var obj = _this.data.obj;
          obj.shopCollect = type;
          _this.setData({
            obj: obj
          })
        }
      }
    });
    
  },
  //商品收藏or取消
  goodsshoucang() {
    if (!wx.getStorageSync('userid')) {
      wx.navigateTo({
        url: '../shouquan/shouquan',
      })
      return false;
    }
    var _this = this;
    var type = this.data.obj.isCollect;
    if (type == 0) {
      type = 1
    } else {
      type = 0
    }
    wx.request({
      url: "https://sdzxiangmu.com/ZJservice", //自己的服务接口地址
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data: {
        cmd: "collect",
        userId: wx.getStorageSync('userid'),
        type: '0', //类型    0-厂家商品收藏 1-厂家店铺收藏
        cid: _this.data.goodsid, //商品id或店铺id
        isCollect: type //0取消1收藏
      },
      success: function success(res) {
        console.log(res.data);
        wx.showToast({
          title: res.data.resultNote,
          icon: 'none',
          duration: 1500
        })
        if (res.data.result == 0) {
          var obj = _this.data.obj;
          obj.isCollect = type;
          _this.setData({
            obj: obj
          })
        }
      }
    });
    
  },
  // 获取商品详情
  getinfo(){
    var _this = this;
    wx.request({
      url: "https://sdzxiangmu.com/ZJservice",
      method:'post',
      header:{
        "content-type": "application/x-www-form-urlencoded"
      },
      data:{
        cmd: 'getTourGoodsDetail',
        goodsId: _this.data.goodsid,
        userId:wx.getStorageSync('userid'),
      },
      success:function(res){
        console.log(res)
        if(res.data.result == 0){
          _this.setData({
            obj:res.data,
            shopid:res.data.shopId,
            guigelist: res.data.goodsSku
            
          })
          setTimeout(function () {
            wx.hideLoading()
          }, 0)

        }else{
          wx.hideLoading()
          wx.showToast({
            title: res.data.resultNote,
            icon: 'none',
            duration: 1000
          })
          setTimeout(function () {
            // wx.navigateBack({
            //   delta: 1
            // })
          }, 1000)
        }


      }
    })
  },
  //本厂优选
  getyouxuan() {
    var _this = this;
    wx.request({
      url: "https://sdzxiangmu.com/ZJservice", //自己的服务接口地址
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data: {
        cmd: "getNgGoodsList",
        townId: wx.getStorageSync('townid'), //区县id  必传字段
        shopId: _this.data.obj.shopId,
        searchKey: "", //搜索关键字
        nowPage: 1 //非必传字段
      },
      success: function success(res) {
        console.log(res.data);
        _this.setData({
          youxuanlist: res.data.goodsList
        })
      }
    });
  },
  //发现好货
  gethaohuo() {
    var _this = this;
    wx.request({
      url: "https://sdzxiangmu.com/ZJservice", //自己的服务接口地址
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data: {
        cmd: "getNgGoodsList",
        townId: wx.getStorageSync('townid'), //区县id  必传字段
        searchKey: "", //搜索关键字
        nowPage: 1 //非必传字段
      },
      success: function success(res) {
        console.log(res.data);
        _this.setData({
          haohuolist: res.data.goodsList
        })
      }
    });
  },
  //获取评价
  getpingjia() {
    var _this = this;
    wx.request({
      url: "https://sdzxiangmu.com/ZJservice", //自己的服务接口地址
      method: "post",
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data: {
        cmd: "getGoodsCmtList",
        goodsId: _this.data.goodsid, //商品id 
        nowPage: 1
      },
      success: function success(res) {
        console.log(res.data);
        _this.setData({
          pingjialist: res.data.commentList
        })
      }
    });
    
  },
  timeFormat: function timeFormat(param) {
    //小于10的格式化函数
    return param < 10 ? "0" + param : param;
  },
  //获取时间
  countDown() {
    var that = this;
    //倒计时函数
    // 获取当前时间，同时得到活动结束时间数组
    var newTime = new Date().getTime();
    var endTimeList = that.data.actEndTimeList;
    var countDownList = [];
    // 对结束时间进行处理渲染到页面
    endTimeList.forEach(function (o) {
      var endTime = new Date(o).getTime();
      var obj = null;
      var time = 0;
      // 如果活动未结束，对时间进行处理
      if (endTime - newTime > 0) {
        var _time = (endTime - newTime) / 1000;
        // 获取天、时、分、秒
        var day = parseInt(_time / (60 * 60 * 24));
        var hou = parseInt(_time % (60 * 60 * 24) / 3600);
        var min = parseInt(_time % (60 * 60 * 24) % 3600 / 60);
        var sec = parseInt(_time % (60 * 60 * 24) % 3600 % 60);
        obj = {
          day: that.timeFormat(day),
          hou: that.timeFormat(hou),
          min: that.timeFormat(min),
          sec: that.timeFormat(sec)
        };
        // obj = obj.day + "天" + obj.hou + ":" + obj.min + ":" + obj.sec;
      } else {
        //活动已结束，全部设置为'00'
        obj = {
          day: "00",
          hou: "00",
          min: "00",
          sec: "00"
        };
        // obj = obj.day + "天" + obj.hou + ":" + obj.min + ":" + obj.sec;
      }

      countDownList.push(obj);

      // console.log(that.data.countDownList)
    });
    that.setData({
      countDownList: countDownList
    })
    // console.log(that.data.countDownList)
    var timer = setTimeout(that.countDown, 1000);
    that.setData({
      timer:timer
    })
    //				console.log(that.countDownList)

  },
  tosuredd() {
    let that = this
    wx.navigateToMiniProgram({
      appId: 'wx9184db707a7ae022',
      path: 'pages/index/index?goodsid=' + that.data.goodsid + '&type=' + that.data.type,
      envVersion: 'release',
      success(res) {
        // 打开其他小程序成功同步触发
        // wx.showToast({
        //   title: '跳转成功'
        // })
      }
    })


  },
  // tosuredd(){
  //   var _this = this;
  //   var obj = this.data.obj;
  //   var list = this.data.sublist;
  //   var type = this.data.type;
  //   console.log(type)
  //   if (list.length == 0){
  //     wx.showToast({
  //       title: '请选择想要购买的商品',
  //       icon: 'none',
  //       duration: 1500
  //     })
  //     return false;
  //   }
  //   if (!wx.getStorageSync('userid')) {
  //     wx.navigateTo({
  //       url: '../shouquan/shouquan',
  //     })
  //     return false;
  //   }
  //   list = JSON.stringify(list)
  //   if(type == 1){ //1单独 2开团
  //     var index = this.data.guigeindex;
  //     // var list = [{
  //     //   goodsId: _this.data.goodsid,          //商品id
  //     //   skuId: _this.data.guigelist[index].skuId,            //商品规格id
  //     //   goodsBuyNum: _this.data.num,      //商品购买数量
  //     // }]
  //     wx.request({
  //       url: "https://sdzxiangmu.com/ZJservice",
  //       method:'post',
  //       header:{
  //         "content-type": "application/x-www-form-urlencoded"
  //       },
  //       data:{
  //         cmd:"toWscBalance",
  //         userId:wx.getStorageSync('userid'),
  //         shopId:_this.data.shopid,
  //         goodsList:list
  //       },
  //       success:function(res) {
  //         console.log(res)
  //         if (res.data.result ==0){
  //           var obj = JSON.stringify(res.data)
  //           wx.navigateTo({
  //             url: '../suredd/suredd?obj=' + obj + '&type=onepin' + '&goodslist=' + list +"&wsType=5" ,
  //           })
  //         }else{
  //           wx.showToast({
  //             title: res.data.resultNote,
  //             icon:'none',
  //             duration:1000
  //           })
  //         }
  //       }
  //     })
  //   }else{  
  //     // 开团
  //     var index = this.data.guigeindex;
  //     wx.request({
  //       url: "https://sdzxiangmu.com/ZJservice", //自己的服务接口地址
  //       method: "post",
  //       header: {
  //         "content-type": "application/x-www-form-urlencoded"
  //       },
  //       data: {
  //         cmd: 'toTourBalance',
  //         flag: 0, // 0-发起团拼 1-参与团拼  
  //         tourId:'',
  //         userId: wx.getStorageSync('userid'),
  //         shopId: _this.data.shopid,
  //         goodsList: list
  //       },
  //       success: function (res) {
  //         console.log(res);
  //         if (res.data.result == 0) {
  //           var obj = JSON.stringify(res.data)
  //           wx.navigateTo({
  //             url: '../suredd/suredd?obj=' + obj + '&type=kaipin'+'&goodslist=' + list,
  //           })
  //           _this.closechoosebox();
  //         } else {
  //           wx.showToast({
  //             title: res.data.resultNote,
  //             icon: 'none',
  //             duration: 1000
  //           })
  //         }

  //       },
  //     });


  //   }
  // },
  // 页面滚动事件监听
  pagescroll(e) {
    // console.log(e.detail.scrollTop + '1111')
    var _this = this;
    var index;
    const query = wx.createSelectorQuery()
    query.selectAll('#maodian1').boundingClientRect()
    query.exec(function (res) {
      if (res[0][0].top <= 200) {
        index = 1;
      }
      const query = wx.createSelectorQuery()
      query.selectAll('#maodian2').boundingClientRect()
      query.exec(function (ress) {
        if (ress[0][0].top <= 200) {
          index = 2;
        }
        const query = wx.createSelectorQuery()
        query.selectAll('#maodian3').boundingClientRect()
        query.exec(function (resss) {
          if (resss[0][0].top <= 200) {
            index = 3;
          }
          const query = wx.createSelectorQuery()
          query.selectAll('#maodian4').boundingClientRect()
          query.exec(function (ressss) {
            if (ressss[0][0].top <= 200) {
              index = 4;
            }
            _this.setData({
              tabindex: index,
            })
          })
        })
      })
    })


  },
  //规格切换
  // changeguige(e) {
  //   this.setData({
  //     guigeindex: e.currentTarget.dataset.index
  //   })
  // },
  //弹出尺码选择窗口
  tanchusizebox(e) {
    var type = e.currentTarget.dataset.type
    if (this.data.obj.goodsState == 1) {
      wx.showToast({
        title: '商品已下架，不支持该操作',
        icon: 'none',
        duration: 1500
      })
      return false;
    }
    var _this = this;
    this.setData({
      blackzt: 'block',
      num: 1,
      guigeindex: 0,
      type:type
    })
    setTimeout(function () {
      _this.setData({
        sizexzbox: true
      })
    }, 100)
  },
  //关闭尺码选择窗口
  closechoosebox() {
    var _this = this;
    this.setData({
      sizexzbox: false
    })
    setTimeout(function () {
      _this.setData({
        blackzt: 'none'
      })
    }, 300)

  },
  // 推荐商品tab切换锚点
  changetab(e) {
    // console.log(e.currentTarget.dataset.current)
    this.setData({
      tabindex: e.currentTarget.dataset.current,
      id: e.currentTarget.dataset.idr
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this
    wx.showLoading({
      title: '加载中',
    })
    _this.setData({
      goodsid: options.goodsid
    })
    if (!wx.getStorageSync('townid')){
      wx.getLocation({
        type: 'gcj02',
        success(res) {
          console.log(res)
          wx.request({
            url: "https://sdzxiangmu.com/ZJservice", //自己的服务接口地址
            method: "post",
            header: {
              "content-type": "application/x-www-form-urlencoded"
            },
            data: {
              cmd: 'getTownId',
              lng: res.longitude,		//地理经度
              lat: res.latitude,		//地理纬度
            },
            success: function success(ress) {
              console.log(ress.data);
              wx.setStorageSync("townid", ress.data.townId);
            }
          });
        }
      })
    }
    wx.request({
      url: "https://sdzxiangmu.com/ZJservice",
      method: 'post',
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      data: {
        cmd: 'getTourGoodsDetail',
        goodsId: _this.data.goodsid,
        userId: wx.getStorageSync('userid'),
        tourId:""
      },
      success: function (res) {
        console.log(res)
        if (res.data.result == 0) {
          res.data.shopScore = parseInt(res.data.shopScore * 100 / 5) + '%';
          for (var i in res.data.goodsSku) {
            res.data.goodsSku[i].num = 0;
          }
          _this.setData({
            obj: res.data,
            guigelist: res.data.goodsSku,
            shopid: res.data.shopId
          })
          setTimeout(function () {
            wx.hideLoading()
          }, 0)
          _this.getimg1(res.data.goodsImg[0])
          if (res.data.goodsImg[1]) {
            _this.getimg2(res.data.goodsImg[1])
          }

        } else {
          wx.hideLoading()
          wx.showToast({
            title: res.data.resultNote,
            icon: 'none',
            duration: 1000
          })
          setTimeout(function () {
            // wx.navigateBack({
            //   delta: 1
            // })
          }, 1000)
        }


      }
    })
    
    // this.getinfo();
    setTimeout(function(){
      _this.getpingjia();
     // _this.getnearlist();
      
    },800)
  }, 

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var _this = this;
    this.getpingjia();
   
    if (this.data.countDownList.length >0){
      _this.countDown();
    }
    var timer = setInterval(function(){
      if (_this.data.obj.shopId && wx.getStorageSync('townid')){
        _this.gethaohuo();
        _this.getyouxuan();
        clearInterval(timer)
      }
    },100)
    
    var timerr = setInterval(function () {
      if(_this.data.type == 5){
        clearInterval(timerr)
      }else{
        if (_this.data.img1) {
          _this.getshareimg();
          clearInterval(timerr)
        }
      }
      
    }, 100)
    
    wx.getSystemInfo({
      success(res) {
        console.log(res)
        _this.setData({
          pageheight: (res.windowHeight * 2) / ((res.windowWidth * 2) / 750) - 200,
        })
      }
    })

    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    var timer = this.data.timer;
    clearTimeout(timer)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var timer = this.data.timer;
    clearTimeout(timer)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  getimg1(imgurl) {
    var _this = this;
    wx.downloadFile({
      url: imgurl, //仅为示例，并非真实的资源
      success(res) {
        // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
        if (res.statusCode === 200) {
          // console.log(res.tempFilePath)
          _this.data.img1 = res.tempFilePath;
          _this.setData({
            img1: res.tempFilePath,
          })
        }
      }
    })
  },
  getimg2(imgurl) {
    console.log(imgurl)
    var _this = this;
    wx.downloadFile({
      url: imgurl, //仅为示例，并非真实的资源
      success(res) {
        // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
        if (res.statusCode === 200) {
          _this.data.img2 = res.tempFilePath;
          _this.setData({
            img2: res.tempFilePath,
          })
        }
      }
    })
  },
  roundRect(ctx, x, y, w, h, r, fillColor, strokeColor) {
    // 画圆角 ctx、x起点、y起点、w宽度、y高度、r圆角半径、fillColor填充颜色、strokeColor边框颜色
    // 开始绘制
    ctx.beginPath()

    // 绘制左上角圆弧 Math.PI = 180度
    // 圆心x起点、圆心y起点、半径、以3点钟方向顺时针旋转后确认的起始弧度、以3点钟方向顺时针旋转后确认的终止弧度
    ctx.arc(x + r, y + r, r, Math.PI, Math.PI * 1.5)

    // 绘制border-top
    // 移动起点位置 x终点、y终点
    ctx.moveTo(x + r, y)
    // 画一条线 x终点、y终点
    ctx.lineTo(x + w - r, y)
    // ctx.lineTo(x + w, y + r)

    // 绘制右上角圆弧
    ctx.arc(x + w - r, y + r, r, Math.PI * 1.5, Math.PI * 2)

    // 绘制border-right
    ctx.lineTo(x + w, y + h - r)
    // ctx.lineTo(x + w - r, y + h)

    // 绘制右下角圆弧
    ctx.arc(x + w - r, y + h - r, r, 0, Math.PI * 0.5)

    // 绘制border-bottom
    ctx.lineTo(x + r, y + h)
    // ctx.lineTo(x, y + h - r)

    // 绘制左下角圆弧
    ctx.arc(x + r, y + h - r, r, Math.PI * 0.5, Math.PI)

    // 绘制border-left
    ctx.lineTo(x, y + r)
    // ctx.lineTo(x + r, y)

    if (fillColor) {
      // 因为边缘描边存在锯齿，最好指定使用 transparent 填充
      ctx.setFillStyle(fillColor)
      // 对绘画区域填充
      ctx.fill()
    }

    if (strokeColor) {
      // 因为边缘描边存在锯齿，最好指定使用 transparent 填充
      ctx.setStrokeStyle(strokeColor)
      // 画出当前路径的边框
      ctx.stroke()
    }
    // 关闭一个路径
    // ctx.closePath()

    // 剪切，剪切之后的绘画绘制剪切区域内进行，需要save与restore
    ctx.clip()
  },
  getshareimg() {
    console.log(11111111111111111111111111)
    var _this = this;
    var type = this.data.type;
    var info = this.data.obj;
    var imglist = info.goodsImg;
    const ctx = wx.createCanvasContext('shareimg')
    console.log(imglist)
    ctx.fillStyle = "#FFFFFF";
    ctx.fillRect(0, 0, 250, 200);
    // 商品名称
    ctx.fillStyle = "#333";
    ctx.setFontSize(18)
    var str = info.goodsName;
    if (str.length > 13) {
      str = str.substring(0, 13) + "...";
    }
    ctx.fillText(str, 10, 150)

    var img1 = this.data.img1;
    var img2 = this.data.img2;
    ctx.drawImage(img1, 10, 10, 110, 110)
    if (img2) {
      ctx.drawImage(img2, 135, 10, 110, 110)
    }
    ctx.fillStyle = "#E22405";
    ctx.setFontSize(16)
    ctx.fillText("￥", 10, 185)




    ctx.setFontSize(22)
    var moneywidth;
    
      ctx.fillText(String(info.goodsCostPrice) + '/' + info.goodsUnit, 24, 185)
      moneywidth = ctx.measureText(String(info.goodsCostPrice)).width
    
    // // console.log(moneywidth)
    ctx.setFontSize(12)
    var discountText;
    
      discountText = '2人团'
    
    var bdColor = '#E22405';
    var bdBackground = 'transparent';
    var bdRadius = 4;
    var textPadding = 6;
    var boxHeight = 18;
    var boxWidth = ctx.measureText(discountText).width + textPadding * 2;
    ctx.fillText(discountText, moneywidth + 75, 183);
    this.roundRect(ctx, moneywidth + 70, 169, boxWidth, boxHeight, bdRadius, bdBackground, bdColor)



    ctx.draw()
    setTimeout(function () {
      wx.canvasToTempFilePath({
        canvasId: 'shareimg',
        success(res) {
          console.log(res.tempFilePath)
          _this.setData({
            shareimg: res.tempFilePath
          })
        }
      })
    }, 500)


  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var _this = this;
    var obj = this.data.obj;
    var shareimg = this.data.shareimg;
    return {
      path: "/pages/huopinxq/huopinxq?goodsid=" + obj.goodsId + '&type=5',
      imageUrl: shareimg,
      title: obj.shopName,
    };
  }
})